﻿using StockMarketTool.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace StockMarketTool.Helper
{
    public class StockQueryHelper
    {
        /*
         * 接口解释：https://www.cnblogs.com/chenjian/p/13975612.html
             */


        /// <summary>
        /// api接口地址
        /// </summary>
        private static string Url = "http://hq.sinajs.cn/list=";
        
        /// <summary>
        /// GET方式发送得结果
        /// </summary>
        /// <param name="url">请求的url</param>
        public static string DoGetRequestSendData(string url)
        {
            HttpWebRequest hwRequest = null;
            HttpWebResponse hwResponse = null;

            string strResult = string.Empty;
            try
            {
                hwRequest = (System.Net.HttpWebRequest)WebRequest.Create(url);
                hwRequest.Timeout = 3000;
                hwRequest.Method = "GET";
                hwRequest.ContentType = "application/javascript; charset=GB18030";
                hwRequest.Host = "hq.sinajs.cn";
                hwRequest.Referer = "https://finance.sina.com.cn/stock/";

            }
            catch (System.Exception err)
            {
                LogHelper.LogError(err,"网络请求出错：");
            }
            try
            {
                hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.GetEncoding("gb2312"));
                strResult = srReader.ReadToEnd();
                srReader.Close();
                hwResponse.Close();
            }
            catch (System.Exception err)
            {
                LogHelper.LogError(err, "网络请求返回出错：");
            }
            return strResult;
        }
        /// <summary>
        /// 根据代码获取行情
        /// </summary>
        /// <param name="codes">代码</param>
        /// <param name="datatype">数据类型，News:行情；Index：指数</param>
        /// <returns></returns>
        public static string GetDataByCode(List<string> codes, StockDataRequestEnum datatype)
        {
            string rs = "";
            StringBuilder sburl = new StringBuilder();
            sburl.Append(Url);
            foreach (string s in codes)
            {
                switch (datatype)
                {
                    case StockDataRequestEnum.News:
                        sburl.Append(s);
                        sburl.Append(",");
                        break;
                    case StockDataRequestEnum.Index:
                        sburl.Append("s_");
                        sburl.Append(s);
                        sburl.Append(",");
                        break;
                    default:
                        break;
                }
            }
            rs = DoGetRequestSendData(sburl.ToString());
            return rs;
        }

    }
}
